"""主要的分类"""

from odoo import models, api, fields
from odoo.tools import float_compare
from odoo.exceptions import UserError, ValidationError



class SupplierCateg(models.Model):
    _name = 'gsp.supplier.categ'
    _inherit = ['gsp.base', 'gsp.manage.base']
    _description = '供应商分类'
    _order = "type"

    type = fields.Selection(required=False)


class CustomerCateg(models.Model):
    _name = 'gsp.customer.categ'
    _inherit = ['gsp.base', 'gsp.manage.base']
    _description = '客户分类'

    type = fields.Selection(required=False)


class ProductCateg(models.Model):
    _name = 'gsp.product.categ'
    _inherit = ['gsp.base', 'gsp.manage.base']
    _description = '商品分类'

    type = fields.Selection(required=False)


class DosageCateg(models.Model):
    _name = 'gsp.dosage.categ'
    _inherit = ['nd.base', 'gsp.base']
    _description = '剂型'


class StorageCondition(models.Model):
    _name = 'gsp.storage.condition'
    _inherit = ['nd.base', 'gsp.base']
    _description = '存储条件'

    type = fields.Selection(required=False)
    humidity_up_limit = fields.Char('湿度上限', readonly=True, states={'draft': [('readonly', False)]}, track_visibility='onchange')
    humidity_low_limit = fields.Char('湿度下限', readonly=True, states={'draft': [('readonly', False)]}, track_visibility='onchange')
    temprature_up_limit = fields.Char('温度上限', readonly=True, states={'draft': [('readonly', False)]}, track_visibility='onchange')
    temprature_low_limit = fields.Char('温度下限', readonly=True, states={'draft': [('readonly', False)]}, track_visibility='onchange')

    _sql_constraints = [
        ('condition_code_unique', 'unique(code)', '存储条件的编号必须唯一!'),
    ]


class MaintainType(models.Model):
    _name = 'gsp.maintain.type'
    _inherit = ['nd.base', 'gsp.base']
    _description = '养护类别'

    period = fields.Integer('养护周期（天数）', readonly=True, states={'draft': [('readonly', False)]}, track_visibility='onchange')


class ExpiringType(models.Model):
    _name = 'gsp.expiring.type'
    _inherit = ['nd.base', 'gsp.base']
    _description = '近效期类别'

    type = fields.Selection(required=False)
    expire_days = fields.Integer('近效期天数', readonly=True, states={'draft': [('readonly', False)]}, track_visibility='onchange')
    stop_days = fields.Integer('到期停售天数', readonly=True, states={'draft': [('readonly', False)]}, track_visibility='onchange')


class Producer(models.Model):
    _name = 'gsp.producer'
    _inherit = ['nd.base', 'gsp.base']
    _description = '生产厂家'

    permit_code = fields.Char('生产许可证号', readonly=True, states={'draft': [('readonly', False)]}, track_visibility='onchange')
    nd_produce_register_num = fields.Char('生产备案凭证编号',  readonly=True, states={'draft': [('readonly', False)]}, track_visibility='onchange')
    files = fields.Many2many('ir.attachment', string='附件', attachment=True)

    @api.model
    def create(self, val):
        val['code'] = self.env['ir.sequence'].next_by_code('gsp.producer.seq')
        return super().create(val)


class InstrumentCateg(models.Model):
    _name = 'gsp.instrument.categ'
    _inherit = ['nd.base', 'gsp.base']
    _description = '医疗器械分类'

    version = fields.Char('分类版本', readonly=True, states={'draft': [('readonly', False)]}, track_visibility='onchange')


class ManageType(models.Model):
    _name = 'gsp.manage.type'
    _inherit = ['nd.base', 'gsp.base']
    _description = '管理类别'


class Certificate(models.Model):
    _name = 'gsp.certificate'
    _inherit = ['mail.thread', 'mail.activity.mixin']
    _description = '注册/备案凭证号'

    name = fields.Char('注册/备案凭证号')
    approve_date = fields.Date('批准日期')
    expire_date = fields.Date('有效期至')
    note = fields.Char('备注')
    files = fields.Many2many('ir.attachment', string='附件', attachment=True)






